package com.xdtech.base.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.base.dao.BaseAreaDao;
import com.xdtech.base.model.BaseArea;
import com.xdtech.base.service.BaseAreaService;
import com.xdtech.base.vo.BaseAreaItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-06-18 14:06:54
 * @since 1.0
 * @see
 */
@Service
public class BaseAreaServiceImpl implements BaseAreaService {
	private Log log = LogFactory.getLog(BaseAreaServiceImpl.class);
	@Autowired
	private BaseAreaDao baseAreaDao;
	@Autowired
	private BaseDao baseDao;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-18 14:06:54
	 * @modified by
	 * @param entity
	 */
	public void save(BaseArea entity) {
		baseAreaDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-18 14:06:54
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<BaseArea> entities) {
		baseAreaDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-18 14:06:54
	 * @modified by
	 * @param entity
	 */
	public void delete(BaseArea entity) {
		baseAreaDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-18 14:06:54
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		baseAreaDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-18 14:06:54
	 * @modified by
	 * @param id
	 * @return
	 */
	public BaseArea get(Long id) {
		return baseAreaDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-18 14:06:54
	 * @modified by
	 * @return
	 */
	public List<BaseArea> getAll() {
		return baseAreaDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-18 14:06:54
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> baseAreas = null;
		long rows = 0;
		if (pg!=null) {
			Page<BaseArea> page = baseAreaDao.findPage(pg,"from BaseArea order by createTime desc", values);
			baseAreas = BeanUtils.copyListProperties(
					BaseAreaItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<BaseArea> baseAreaList = baseAreaDao.find("from BaseArea order by id desc", values);
			baseAreas = BeanUtils.copyListProperties(
					BaseAreaItem.class, baseAreaList);
			rows = baseAreas.size();
		}
		results.put("total", rows);
		results.put("rows", baseAreas);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-18 14:06:54
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateBaseArea(BaseAreaItem item) {
		BaseArea baseArea = null;
		if (item.getId()==null) {
			baseArea = new BaseArea();
		}else {
			baseArea = baseAreaDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(baseArea, item);
		baseAreaDao.save(baseArea);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-18 14:06:54
	 * @modified by
	 * @param baseAreaId
	 * @return
	 */
	public BaseAreaItem loadBaseAreaItem(Long baseAreaId) {
		BaseArea baseArea = baseAreaDao.get(baseAreaId);
		return BaseAreaItem.createItem(baseArea);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-18 14:06:54
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteBaseAreaInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-18 14:06:54
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteBaseAreaInfo(List<Long> baseAreaIds) {
		for (Long id : baseAreaIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-18 14:06:54
	 * @modified by
	 * @return
	 */
	@Override
	public List<BaseAreaItem> loadItems() {
		List<BaseArea> baseAreas = getAll();
		return BaseAreaItem.createItems(baseAreas);
	}

}
